<?php
namespace app\admin\controller;

use app\admin\service\Auth;
use app\common\gateway\data\GroupSender;
use app\common\gateway\data\JsonData;
use DI\Annotation\Inject;
use GatewayWorker\Lib\Gateway;
use support\annotation\NoNeedRight;
use support\bootstrap\Cache;
use support\command\workerman\GatewayWorker;
use support\Request;
use support\util\TreeArray;
use Workerman\Lib\Timer;

/**
 * @NoNeedRight()
 */
class Index extends BaseController
{
    /**
     * @var Auth
     * @Inject()
     */
    protected $service = null;

    /**
     * @var \app\admin\service\Menu
     * @Inject()
     */
    protected $menuService = null;

    public function index(Request $request) {
        return view('index/index');
    }

    public function menu()
    {
        $menuList = $this->service->getMenuList();
        $menuList = $menuList->toArray();
        foreach ($menuList as &$menuRow) {
            $menuRow['type'] = $menuRow['pid'] == 0 ? 0 : 1;
            $menuRow['openType'] = $menuRow['target'];
            $menuRow['href'] = $menuRow['rule'];
            $menuRow['icon'] = "layui-icon " . $menuRow['icon'];
        }
        unset($menuRow);
        $tree = TreeArray::instance();
        $tree->init($menuList);
        return json($tree->getTreeArray(0, '', 'children'));
    }

    public function init()
    {
        $menuArr = $this->service->getMenuList();
        $tree = TreeArray::instance();
        $tree->init($menuArr);
        $menuTree = $tree->getTreeArray(0, '', 'children');
        $defaultMenu = $menuTree[0]['children'][0];
        return json([
            "logo" => [
                "title" => "cli Admin",
                "image" => admin_theme_url('admin/images/logo.png')
            ],
            "menu" => [
                "data" => "/admin/index/menu",
                "accordion" => true,
                "control" => true,
                "select" => $defaultMenu['id'] // 选中id
            ],
            "tab" => [
                "muiltTab" => true,
                "keepState" => true,
                "tabMax" => 30,
                "index" => [
                    "id" => $defaultMenu['id'],
                    "href" => $defaultMenu['rule'],
                    "title" => $defaultMenu['title']
                ]
            ],
            "theme" => [
                "defaultColor" => "2",
                "defaultMenu" => "dark-theme",
                "allowCustom" => true
            ],
            "colors" => [
                [
                    "id" => "1",
                    "color" => "#2d8cf0"
                ],
                [
                    "id" => "2",
                    "color" => "#5FB878"
                ],
                [
                    "id" => "3",
                    "color" => "#1E9FFF"
                ],
                [
                    "id" => "4",
                    "color" => "#FFB800"
                ],
                [
                    "id" => "5",
                    "color" => "darkgray"
                ]
            ],
            "other" => [
                "keepLoad" => 1200,
                "autoHead" => false
            ]
        ]);
    }

    public function console(Request $request)
    {
        $view = $request->get('view');
        if ($view == '') {
            $view = 'console';
        }
        return view('index/' . $view, [
            'ip' => $request->getLocalIp()
        ]);
    }

    public function bindwk(Request $request)
    {
        $client_id = $request->post('client_id');
        if ( !Gateway::isOnline($client_id) ) {
            $this->error("客户端不存在！");
        }
        Gateway::bindUid($client_id, $this->service->getUserModel()->id);
        $token = md5(md5($client_id) . GatewayWorker::TOKEN_KEY);
        $this->success("绑定成功!", [
            'token' => $token
        ]);
    }

    public function rmcache()
    {
        Cache::instance()->deleteAll();
        $this->menuService->deleteCache();
        $this->success("清除成功!");
    }

    /**
     * @NoNeedRight()
     */
    public function sysinfo()
    {
        return view('index/sysinfo');
    }

    /**
     * @param Request $request
     * @NoNeedRight()
     */
    public function joingroup(Request $request)
    {
        $client_id = $request->post('client_id');
        $group = $request->post('group');
        Gateway::joinGroup($client_id, $group);
        $this->success("加入成功");
    }

    public function logout()
    {
        $this->service->logout();;
        $this->success("退出成功!", [
            'url' => $this->loginedRedirectUrl
        ]);
    }
}
